package com.yuneec.fly.dialogs;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.yuneec.fly.R;

/**
 * Created by jw.gu on 2016/6/25.
 */
@SuppressLint("ValidFragment")
public class LoginWifiDialog extends BaseDialogFragment {
    private String ssid;
    private OnConfirmListener onConfirmListener;
    private OnCancelListener onCancelListener;

    public LoginWifiDialog(String ssid, OnConfirmListener onConfirmListener, OnCancelListener onCancelListener) {
        super();
        this.ssid = ssid;
        this.onConfirmListener = onConfirmListener;
        this.onCancelListener = onCancelListener;
    }

    public static interface OnConfirmListener {
        public void onConfirm(BaseDialogFragment dialogFragment, String password);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.dialog_login_wifi, container);
        final CheckBox show_password = (CheckBox) view.findViewById(R.id.show_password);
        final TextView name = (TextView) view.findViewById(R.id.ssid_name);
        final EditText password = (EditText) view.findViewById(R.id.ssid_password);

        name.setText(ssid);

        show_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
                password.setSelection(password.getText().length());
            }
        });

        View.OnClickListener cancelListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                password.clearFocus();
                if (onCancelListener != null) {
                    onCancelListener.onCancel(LoginWifiDialog.this);
                }
                dismiss();
            }
        };
        view.findViewById(R.id.confirmBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (password.getEditableText().toString().length() > 0) {
                    password.clearFocus();
                    if (onConfirmListener != null) {
                        onConfirmListener.onConfirm(LoginWifiDialog.this, password.getEditableText().toString());
                    }
                }
            }
        });
        view.findViewById(R.id.cancelBtn).setOnClickListener(cancelListener);
        view.findViewById(R.id.cancelIV).setOnClickListener(cancelListener);
        return view;
    }
}
